//
//  ViewController.m
//  ali
//
//  Created by mac on 2017/8/29.
//  Copyright © 2017年 mac. All rights reserved.

#import "ViewController.h"
#import <AlipaySDK/AlipaySDK.h>

@interface ViewController ()

- (IBAction)aliBtn:(id)sender;

@property (weak, nonatomic) IBOutlet UITextField *money;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
}


- (IBAction)aliBtn:(id)sender {
    
    NSString *before = @"http://monitor.yuncentry.com/index.php/api/Alinotify/ali/shop_price/";
    NSString *mPrice = _money.text;
    NSString *after = @"/priceid/467/uids/2/order_sn/1500539705";
    
    NSString *urlString = [NSString stringWithFormat:@"%@%@%@",before, mPrice,after];
    NSLog(@"urlString = %@", urlString);
    
    static NSString *orderString;
    
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[[NSURL alloc] initWithString:urlString]];
    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request
                                            completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
                                                orderString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                                NSLog(@"Get string from server");
                                            }];
    [task resume];
    
    
    
    //应用注册scheme,在AliSDKDemo-Info.plist定义URL types
    NSString *appScheme = @"alipaych";
    
//    NSString *orderString = @"app_id=2017062707581146&biz_content=%7B%22timeout_express%22%3A%2230m%22%2C%22seller_id%22%3A%221660075632%40qq.com%22%2C%22product_code%22%3A%22QUICK_MSECURITY_PAY%22%2C%22total_amount%22%3A%220.01%22%2C%22priceid%22%3A%22467%22%2C%22uids%22%3A%222%22%2C%22subject%22%3A%22%5Cu76d1%5Cu63a7APP%5Cu5957%5Cu9910%5Cu8d2d%5Cu4e70%22%2C%22body%22%3A%22%5Cu5546%5Cu54c1%5Cu8d2d%5Cu4e70%5C%2F%5Cu5145%5Cu503c%22%2C%22out_trade_no%22%3A%221500539705%22%7D&charset=UTF-8&format=json&method=alipay.trade.app.pay&notify_url=http%3A%2F%2Fmonitor.yuncentry.com%2Fnotify.php&sign_type=RSA&timestamp=2017-08-30%2011%3A38%3A56&version=1.0&sign=jksDSHpqPQC74NQ6Xr2bj2nMiWt6k833jK1MqlF1PsPVLYfABmkpwjjVFBWuReZSvGtSFxu41v1IPzwZZXRYs1cEBzcHD1GyRl%2BCY7Mxz%2FoWrIJ%2FiAQjg1Vc3msy06o1gb9aZvsRz0V1c0XmKbzxkHuRglnof%2FSSJ4khKIc3rMw%3D";
    
    if ((urlString != NULL) && (orderString != NULL)) {
        // NOTE: 调用支付结果开始支付
        NSLog(@"Enter the payment interface");
        [[AlipaySDK defaultService] payOrder:orderString fromScheme:appScheme callback:^(NSDictionary *resultDic) {
            NSLog(@"reslut = %@",resultDic);
        }];
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
